热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

百度地图IOS平台开发--自定义大头钉以及泡泡

最近在做百度地图的相关开发,查阅了很多资料,包括csdn上的很多自定义视图方面的博客,因为和自己的地图设计要求不太一样,因此单独写了这篇博客,希望能给类似需求的同学一点帮助;简单的说一

最近在做百度地图的相关开发,查阅了很多资料,包括csdn上的很多自定义视图方面的博客,因为和自己的地图设计要求不太一样,因此单独写了这篇博客,希望能给类似需求的同学一点帮助 ;

简单的说一下自定义视图的设计要求:

点击大头针的弹出泡泡视图中点击“选择”按钮,会触发两个变化:
1.按钮变成“取消选择” 
 2.对应的大头针图标上出现一个勾选的图片

草图如下:

下面开始正经地瞎说了(郭德纲的经典台词):

首先相信大家也大概知道MapView的基本属性和一些常用的代理方法,下面就挑我用到的几个属性和代理方法简单的

说明一下:

1 .属性:

(1)coordinate:这个属性是自带的,用来表示经纬度

(2)pointCalloutInfo:这个属性是自定义的,用来保存annotation的一些信息,诸如用户头像,用户名之类的

2.代理方法

(1)- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotationnnotation;

   注意这个代理方法只要mapView addAnnotation一次就自动被调用一次

(2)- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view

(3)- (void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view

         这里需要特别强调一下(2),(3)这两个代理方法的调用时序:

在我的这个项目里,是这样的。首先在viewDidLoad中初始化annotation,然后mapView addAnnotation,上面已说明,此时mapview立即调用(1)这个代理方法加载,那么此时我们就可以在mapview中看见大头钉,此时如果我们点击大头钉,就会调用方法(2),弹出大头钉对应的泡泡视图,接下来注意啦!!!如果我们紧接着点击大头钉是不会调用任何方法的,但如果我们紧接着点击的是泡泡视图,那么将先调用方法(3),然后再调用方法(2),有人问了,如果我们既不是点击大头钉和泡泡视图,而是点击地图空白处呢,那么很简单,系统会调用方法(3)。其实大家仔细琢磨一样,还是会想通的。


好吧,上面很啰嗦的说了很多,接下来进入主题,自定义视图。分为两部分,自定义大头钉和自定义泡泡视图

       3.自定义大头钉

CustomPinAnnotation.h      

#import "BMKPointAnnotation.h"
@interfaceCustomPointAnnotation : BMKPointAnnotation
@property (nonatomic,retain)NSDictionary*pointCalloutInfo;
@end

CustomPinAnnotation.m

#import "CustomPointAnnotation.h"
@implementation CustomPointAnnotation
@synthesizepointCalloutInfo;
@end


CustomPinAnnotationView.h

#import "BMKAnnotationView.h"
#import "CustomPinCell.h"
@interfaceCustomPointAnnotationView :BMKAnnotationView
@property (nonatomic,strong)UIView*contentView;//use to add view
@property (nonatomic,strong)CustomPinCell*customPinCell;
@end

CustomPinAnnotationView.m

#import "CustomPointAnnotationView.h"
@implementation CustomPointAnnotationView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier{


self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];//弹出框最外面的父view
self.canShowCallout = NO;
self.frame = CGRectMake(0, 0, 60, 71);

UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
subView.backgroundColor = [UIColor clearColor];//弹出框里面的子view
[self addSubview:subView];
self.cOntentView= subView;
}
return self;

}

@end


- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation{


if ([annotation isKindOfClass:[CustomPointAnnotation class]]) {

static NSString *ID = @"reuseId";
CustomPointAnnotationView *customPointAnnotatiOnView= (CustomPointAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if (!customPointAnnotationView) {
customPointAnnotatiOnView= [[CustomPointAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:ID];
}
CustomPinCell *cell = [[NSBundle mainBundle] loadNibNamed:@"CustomPinCell" owner:self options:nil][0];
CustomPointAnnotation *pointAnnotation = (CustomPointAnnotation *)annotation;
cell = [cell instancePinCellWithDictionary:pointAnnotation.pointCalloutInfo];
[customPointAnnotationView.contentView addSubview:cell];
customPointAnnotationView.customPinCell = cell;

return customPointAnnotationView;


}else if ([annotation isKindOfClass:[CalloutMapAnnotation class]]){


CalloutMapAnnotation *calloutAnnotation = (CalloutMapAnnotation*)annotation;
static NSString *popViewId = @"calloutview";
CalloutAnnotationView *calloutannotatiOnview= (CalloutAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:popViewId];

if (!calloutannotationview) {

calloutannotatiOnview= [[CalloutAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:popViewId];
}

CustomPopCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomPopCell" owner:self options:nil] objectAtIndex:0];

CustomPinCell *pipCell = [pointerArray objectAtIndex:wholeIndex];
if (pipCell.selectFlag) {

[cell.selectBtn setTitle:@"取消选择" forState:(UIControlStateNormal)];
}else{
[cell.selectBtn setTitle:@"选择" forState:(UIControlStateNormal)];
}

[calloutannotationview.contentView addSubview:cell];
calloutannotationview.customPopCell = cell;

//开始设置添加marker时的赋值
calloutannotatiOnview= [calloutannotationview instanceAnnotationViewWithDictionary:calloutAnnotation.locationInfoDict];
return calloutannotationview;

}

return nil;
}

- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view{

if ([view.annotation isKindOfClass:[CustomPointAnnotation class]]) {

if (_calloutMapAnnotation.coordinate.latitude == view.annotation.coordinate.latitude&&
_calloutMapAnnotation.coordinate.lOngitude== view.annotation.coordinate.longitude) {
return;

}
if (_calloutMapAnnotation) {
[mapView removeAnnotation:_calloutMapAnnotation];
_calloutMapAnnotation=nil;
}

_calloutMapAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude andLongitude:view.annotation.coordinate.longitude];

_calloutMapAnnotation.locatiOnInfoDict= annn.pointCalloutInfo;

[mapView addAnnotation:_calloutMapAnnotation];

[mapView setCenterCoordinate:view.annotation.coordinate animated:YES];

}
}

- (void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view{

if (_calloutMapAnnotation&&![view isKindOfClass:[CalloutAnnotationView class]]) {

if (_calloutMapAnnotation.coordinate.latitude == view.annotation.coordinate.latitude&&
_calloutMapAnnotation.coordinate.lOngitude== view.annotation.coordinate.longitude) {

[mapView removeAnnotation:_calloutMapAnnotation];
_calloutMapAnnotation = nil;
}
}
}


注意,上面提到的PinCell其实是一个UIView

       4.自定义泡泡视图

完全和上面的自定义大头针一样

至此,基本什么问题都解决了









推荐阅读
  • TCP长连接设备管理平台:架构与功能概览
    本文介绍了基于TCP长连接的设备管理平台的设计理念、技术选型及主要功能模块。最初,项目旨在实现简单的协议测试,但随着需求扩展,逐步演变为一个完整的前后端分离系统。 ... [详细]
  • 本文介绍了在使用Visual Studio 2015进行项目开发时,遇到类向导弹出“异常来自 HRESULT:0x8CE0000B”错误的解决方案。通过具体步骤和实践经验,帮助开发者快速排查并解决问题。 ... [详细]
  • 如何在PHPcms网站中添加广告
    本文详细介绍了在PHPcms网站后台添加广告的方法,涵盖多种常见的广告形式,如百度广告和Google广告,并提供了相关设置的步骤。同时,文章还探讨了优化网站流量的SEO策略。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 基于KVM的SRIOV直通配置及性能测试
    SRIOV介绍、VF直通配置,以及包转发率性能测试小慢哥的原创文章,欢迎转载目录?1.SRIOV介绍?2.环境说明?3.开启SRIOV?4.生成VF?5.VF ... [详细]
  • 揭秘:为何我的网名是老紫竹
    本文详细解释了作者为何选择“老紫竹”作为网名,从个人喜好到网络经历,以及与紫竹植物的渊源。 ... [详细]
  • 本文介绍了ArcXML配置文件的分类及其在不同服务中的应用,详细解释了地图配置文件的结构和功能,包括其在Image Service、Feature Service以及ArcMap Server中的使用方法。 ... [详细]
  • 本文介绍通过百度地图获取当前位置建筑物名称的具体步骤和方法,帮助用户更便捷地了解所在地点的信息。 ... [详细]
  • 本文介绍如何在Django的管理后台中为特定模型添加自定义地图功能,例如使用百度地图API根据场馆名称获取并存储地理坐标。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • C++: 实现基于类的四面体体积计算
    本文介绍如何使用C++编程语言,通过定义类和方法来计算由四个三维坐标点构成的四面体体积。文中详细解释了四面体体积的数学公式,并提供了两种不同的实现方式。 ... [详细]
  • 本题涉及一棵由N个节点组成的树(共有N-1条边),初始时所有节点均为白色。题目要求处理两种操作:一是改变某个节点的颜色(从白变黑或从黑变白);二是查询从根节点到指定节点路径上的第一个黑色节点,若无则输出-1。 ... [详细]
  • 并发编程:深入理解设计原理与优化
    本文探讨了并发编程中的关键设计原则,特别是Java内存模型(JMM)的happens-before规则及其对多线程编程的影响。文章详细介绍了DCL双重检查锁定模式的问题及解决方案,并总结了不同处理器和内存模型之间的关系,旨在为程序员提供更深入的理解和最佳实践。 ... [详细]
  • 技术人员转型项目管理:常见思维误区与挑战解析
    本文探讨了技术人员在向项目管理角色转变过程中常见的思维误区和困惑,分析了如何有效管理项目中的事务和人员,提供了实用的解决方案。 ... [详细]
  • 探索1000以内的完美数:因数和等于自身
    本文探讨了如何在1000以内找到所有完美数,即一个数的因数(不包括自身)之和等于该数本身。例如,6是一个完美数,因为1 + 2 + 3 = 6。通过编程实现这一过程,可以更好地理解完美数的特性。 ... [详细]
author-avatar
我不是咸鱼仔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有